//
//  FindOutViewController.m
//  lvtu
//
//  Created by Apple on 12-3-9.
//  Copyright (c) 2012年 __MyCompanyName__. All rights reserved.
//

#import "FindOutViewController.h"
#import <QuartzCore/QuartzCore.h>

@implementation FindOutViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)didReceiveMemoryWarning
{
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc that aren't in use.
}

#pragma mark - View lifecycle

/*
// Implement loadView to create a view hierarchy programmatically, without using a nib.
- (void)loadView
{
}
*/

// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad
{
    [super viewDidLoad];
//    self.title = @"找回密码";
    UILabel* tempLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 200, 44)];
    [tempLabel setText:@"找回密码"];
    [tempLabel setBackgroundColor:[UIColor clearColor]];
    [tempLabel setTextAlignment:UITextAlignmentCenter];
    [tempLabel setTextColor:[UIColor colorWithRed:111/255.0 green:117/255.0 blue:81/255.0 alpha:1.0]];
    [tempLabel setFont:[UIFont boldSystemFontOfSize:20]];
    self.navigationItem.titleView = tempLabel;
    [tempLabel release];
    tempLabel = nil;
    
    self.navigationController.navigationBarHidden = NO;
    
    UIImage* backImage = [UIImage imageNamed:@"btn_back_normal.png"];
    UIButton* leftbutton = [UIButton buttonWithType:UIButtonTypeCustom];
    leftbutton.frame = CGRectMake(0, 0, backImage.size.width, backImage.size.height);
    [leftbutton setBackgroundImage:backImage forState:UIControlStateNormal];
    [leftbutton setBackgroundImage:[UIImage imageNamed:@"btn_back_press.png"] forState:UIControlStateHighlighted];
    [leftbutton addTarget:self action:@selector(returnButtonPressed:) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem* findoutButtonItem = [[UIBarButtonItem alloc] initWithCustomView:leftbutton];
    //signupButton.image = [UIImage imageNamed:@"btn_finish_normal.png"];
    self.navigationItem.leftBarButtonItem = findoutButtonItem;
    [findoutButtonItem release];
    
    UILabel* mailLabel = [[UILabel alloc] initWithFrame:CGRectMake(50, 30, 50, 30)];
    mailLabel.text = @"邮箱 :";
    mailLabel.font = [UIFont systemFontOfSize:13];
    mailLabel.backgroundColor = [UIColor clearColor];
    mailLabel.tag = 1000;
    [self.view addSubview:mailLabel];
    [mailLabel release];
    
    mailTextField = [[UITextField alloc] init];
    UIImage* TextFieldImage = [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"bg_imput.png" ofType:nil]];
    UIImageView* textFieldBackImageView = [[UIImageView alloc] initWithFrame:CGRectMake(44, 60, 230, 36)];
    textFieldBackImageView.image = TextFieldImage;
    textFieldBackImageView.tag = 1001;
    [self.view addSubview:textFieldBackImageView];
    [textFieldBackImageView release];
    
    [mailTextField setFrame:CGRectMake(44, 60, 220, 36)];
    //[mailTextField setBackground:TextFieldImage];
    mailTextField.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;                
    [mailTextField setAutocorrectionType:UITextAutocorrectionTypeDefault];
    [mailTextField setReturnKeyType:UIReturnKeyDefault];
    [mailTextField setFont:[UIFont boldSystemFontOfSize:12]];
    [mailTextField setDelegate:self];
    [mailTextField setTextColor:[UIColor grayColor]];
    [mailTextField setPlaceholder:@"你在注册时使用的邮箱地址"];
    [mailTextField setKeyboardType:UIKeyboardTypeURL];
    [mailTextField setClearButtonMode:UITextFieldViewModeWhileEditing];
    [mailTextField setClipsToBounds:YES];
    [self.view addSubview:mailTextField];
    [mailTextField release];
    
    UIButton* buttonFindOut = [UIButton buttonWithType:UIButtonTypeCustom];
	buttonFindOut.frame = CGRectMake(60, 140, 200, 45);
    [buttonFindOut setBackgroundImage:[UIImage imageNamed:@"btn_done_normal.png"] forState:UIControlStateNormal];
    [buttonFindOut setBackgroundImage:[UIImage imageNamed:@"btn_done_press.png"] forState:UIControlStateHighlighted];
    [buttonFindOut setTitle:@"确认" forState:UIControlStateNormal];
	[buttonFindOut addTarget:self action:@selector(findOutButtonPressed:) forControlEvents:UIControlEventTouchUpInside];
    buttonFindOut.tag = 1002;
	[self.view addSubview:buttonFindOut];
}

- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
//    return (interfaceOrientation == UIInterfaceOrientationPortrait);
    return NO;
}

- (void)findOutButtonPressed:(id)sender
{
    NSDictionary* info = [[NSDictionary alloc] initWithObjectsAndKeys:mailTextField.text,@"email",nil];
    CoreMsg* msg = [[CoreMsg alloc] init];
    msg.data = FINDOUT;
    msg.ui_delegate = self;
    
    [[NetDataSource shareInstance]postTo:@"http://www.wxlvtu.com/?a=do_find_password" postInfo:info userInfo:msg];
}

- (void)returnButtonPressed:(id)sender
{
    [self.navigationController popViewControllerAnimated:YES];
}
#pragma -mark DataSourceDelegate

- (void)onDataSourceSucCallback:(CoreMsg *)coreMsg withData:(id)data
{
    if ([coreMsg.data isEqual: FINDOUT]) {
        //[self.navigationController popViewControllerAnimated:YES];
        UILabel* label = (UILabel*)[self.view viewWithTag:1000];
        [label removeFromSuperview];
        
        UIImageView* textBg = (UIImageView*)[self.view viewWithTag:1001];
        [textBg removeFromSuperview];
        
        [mailTextField removeFromSuperview];
        
        UIButton* button = (UIButton*)[self.view viewWithTag:1002];
        [button setTitle:@"立即登录" forState:UIControlStateNormal];
        [button addTarget:self action:@selector(returnButtonPressed:) forControlEvents:UIControlEventTouchUpInside];
        
        UIImageView* icon = [[UIImageView alloc] initWithFrame:CGRectMake(50, 65, 20, 20)];
        icon.backgroundColor = [UIColor clearColor];
        icon.image = [UIImage imageNamed:@"icn_tip.png"];
        [self.view addSubview:icon];
        [icon release];
        
        UILabel* tip = [[UILabel alloc] initWithFrame:CGRectMake(80, 60, 260, 30)];
        tip.backgroundColor = [UIColor clearColor];
        tip.textColor = [UIColor orangeColor];
        tip.font = [UIFont systemFontOfSize:15];
        tip.text = @"密码以成功发送至你的注册邮箱";
        [self.view addSubview:tip];
        [tip release];
    }
}

- (void)onDataSourceFailCallback:(CoreMsg *)coreMsg withData:(id)data
{
    if ([coreMsg.data isEqual: FINDOUT]) {
        UIAlertView* alert = [[UIAlertView alloc] initWithTitle:@"提示" message:data delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
        alert.tag = 2001;
        [alert show];
        [alert release];
    }
}

#pragma -mark UITextFieldDelegate
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{
    if (textField == mailTextField && [mailTextField.text isEqualToString:@"你在注册时使用的邮箱地址"])
    {
        [mailTextField setText:@""];
        [mailTextField setTextColor:[UIColor blackColor]];
    }
    
    return YES;
}

- (void)textFieldDidBeginEditing:(UITextField *)textField
{
}

- (void)textFieldDidEndEditing:(UITextField *)textField
{
    [mailTextField resignFirstResponder];
    if ( textField == mailTextField && [mailTextField.text isEqualToString:@""])
    {
        [mailTextField setPlaceholder:@"你在注册时使用的邮箱地址"];
        [mailTextField setTextColor:[UIColor grayColor]];
    }
}

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
    if (1 == range.length) {//按下回格键
        return YES;
    }
    
    if ([string isEqualToString:@"\n"])
    {//按下return键
        //这里隐藏键盘，不做任何处理
        [textField resignFirstResponder];
        //        [self willLoginWeibo];
        return NO;
    }
    else
    {
        return YES;
    }
}

- (BOOL)textFieldShouldClear:(UITextField *)textField
{
    if (textField == mailTextField)
    {
        [mailTextField setText:@""];
        [mailTextField setPlaceholder:@"你在注册时使用的邮箱地址"];
        [mailTextField setTextColor:[UIColor grayColor]];
    }
    return YES;
}

@end
